﻿<template>
<div>
  <el-dialog
    :title="!dataForm.unitCode ? $t('handle.add') :  btnshow ? '修改':'详情'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <div v-loading = loading>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="150px">
    <el-form-item label="选择地址" prop="addressText">
     <el-input v-model="dataForm.addressText" placeholder="请选择标准地址" :disabled="true" style="width:90%"></el-input><el-button style="width:10%" type="info" :disabled="isInput" plain @click="addressDialogOpen()">选择</el-button>
    </el-form-item>
    <el-form-item label="单位名称" prop="name">
      <el-input v-model="dataForm.name" :disabled="isInput" placeholder="请输入单位名称"></el-input>
    </el-form-item>
    <el-form-item label="统一社会编码" prop="businessLicense">
      <el-input v-model="dataForm.businessLicense" :disabled="isInput" placeholder="请输入统一社会编码"></el-input>
    </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="单位属性" prop="dwsxValue">
      <el-select v-model="dataForm.dwsxValue" :disabled="isInput" placeholder="请选择" style="width:100%">
        <el-option
          v-for="item in dwsxOptions"
          :key="item.id"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="单位类别" prop="qylbValue">
      <el-select v-model="dataForm.qylbValue" :disabled="isInput" placeholder="请选择" style="width:100%">
        <el-option
          v-for="item in qylbOptions"
          :key="item.id"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item label="是否有监控" prop="monitoringValue">
            <el-select v-model="dataForm.monitoringValue" :disabled="isInput" placeholder="请选择" style="width:100%">
              <el-option
                v-for="item in monitoringOptions"
                :key="item.id"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="注册类型" prop="qyczlxValue">
      <el-select v-model="dataForm.qyczlxValue" placeholder="请选择" :disabled="isInput" style="width:100%">
        <el-option
          v-for="item in qyczlxOptions"
          :key="item.id"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
<el-form-item label="控股情况" prop="kgqkValue">
      <el-select v-model="dataForm.kgqkValue" :disabled="isInput" placeholder="请选择" style="width:100%">
        <el-option
          v-for="item in kgqkOptions"
          :key="item.id"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
        </el-col>
        <el-col :span="12">
           <el-form-item label="经营范围" prop="jyfwValue">
      <el-select v-model="dataForm.jyfwValue" :disabled="isInput" placeholder="请选择" style="width:100%">
        <el-option
          v-for="item in jyfwOptions"
          :key="item.id"
          :label="item.label"
          :value="item.value">
        </el-option>
      </el-select>
    </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div style="text-align:right;">
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{ $t('handle.cancel') }}</el-button>
      <el-button type="primary" v-show="btnshow" @click="dataFormSubmit()">{{ $t('handle.confirm') }}</el-button>
    </span>
    </div>
  </div>
  </el-dialog>
    <address-Dialog v-if="addressVisible" ref="addressDialog" @receiveParameters="getAddressParameters"></address-Dialog>
</div>
</template>

<script>
  import { getDictList } from '@/utils'
  import HouseDialog from '../dialog/houseDialogMore'
  import AddressDialog from '../dialog/addressDialog'
  // import AddressDialog from '../addressDialog'
  export default {
    data () {
        let checkBusiness = (rule, value, callback) => {
          if(!this.validateBusiness(value)){
            return callback(new Error("不是有效的统一社会信用编码！"));
          }else {
            // this.$http({
            //   // url: this.$http.adornUrl(`/dgactualunit/info/${this.dataForm.unitCode}`),
            //   url: this.urlSuf+`/dgregisterunit/infobybusinesslicense/${value}`,
            //   method: 'get',
            //   params: this.$http.adornParams()
            // }).then(({data}) => {
            //   if (data && data.dgRegisterUnit === null) {
            //     return callback();
            //   }else{
            //     return callback(new Error("统一社会信用编码已经存在"));
            //   }
            // })
             return callback();
          }
          // if(value ){}
        // if (!this.validateBusiness(value)) {
        //   this.dataForm.sex = "";
        //   this.dataForm.birthdate = "";
        //   return callback(new Error("身份证号格式错误，请检查输入"));
        // } else {
        //   existIdCard(value, this.dataForm.populationCoding).then(({ data }) => {
        //     if (data.count > 0) {
        //       return callback(new Error("身份证号码已经存在"));
        //     } else {
        //       this.dataForm.sex = this.getSexByIdCard(value);
        //       this.dataForm.birthdate = this.getBirthday(value);
        //       return callback();
        //     }
        //   });
        // }
      };
      return {
        loading :false,
        visible: false,
        isInput: false,
        dataForm: {
          unitCode: '',
          addressId: '',
          addressText: '',
          name: '',
          dwsxValue: '', // 单位属性选中项
          qyczlxValue: '', // 单位注册类型选中项
          qylbValue: '', // 单位类别
          kgqkValue: '', // 控股情况数据
          jyfwValue: '',  // 经营范围选中项
          monitoringValue: '', // 是否有监控选中项
          businessLicense: '',
          type: '',
          // houseName: '',
          btnshow: true
        },
        houseAndUnit: {
          houseId: '',
          // houseName: '',
          type:'',
          relationId:''
        },
        dataRule: {
          addressText: [
            { required: true, message: '请选择地址名称', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '单位名称不能为空', trigger: 'blur' }
          ],
          dwsxValue: [
            { required: true, message: '单位属性不能为空', trigger: 'blur' }
          ],
          qylbValue: [
            { required: true, message: '单位类别不能为空', trigger: 'blur' }
          ],
          // qyczlxValue: [
          //   { required: true, message: '单位注册类型不能为空', trigger: 'blur' }
          // ],
          // kgqkValue: [
          //   { required: true, message: '控股情况不能为空', trigger: 'blur' }
          // ],
          // jyfwValue: [
          //   { required: true, message: '经营范围不能为空', trigger: 'blur' }
          // ],
          // monitoringValue: [
          //   { required: true, message: '是否有监控不能为空', trigger: 'blur' }
          // ],
          // type: [
          //   { required: true, message: '人房关系不能为空', trigger: 'blur' }
          // ],
          businessLicense: [
            { required: true, message: '营业执照号不能为空', trigger: 'blur' }
            // { validator: checkBusiness, required: true, trigger: "blur" }
          ]
        },
        dwsxOptions: [], // 单位属性数据
        qylbOptions: [], // 单位类别数据
        qyczlxOptions: [], // 单位注册类型数据
        kgqkOptions: [], // 控股情况数据
        jyfwOptions: [], // 经营范围数据
        options: [],
        btnshow: true,
        addressVisible: false,
        monitoringOptions: [] // 是否有监控数据
      }
    },
    computed: {
      regionId() {
        return this.$store.state.address.id;
      }
    },
    // components: {
    //   'address-Dialog': AddressDialog
    // },
    components: {
      'house-Dialog': HouseDialog,
      'address-Dialog': AddressDialog
    },
    methods: {
      init (id,detail) {
        this.getSelectData()
        this.initFrom(id, detail)
      },
      initFrom (id, detail) {
        this.btnshow = true
        this.dataForm.unitCode = id || 0
        this.visible = true
        this.isInput = false
        if (detail) {
          this.btnshow = false
          this.isInput = true
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.unitCode) {
            this.$http({
              // url: this.$http.adornUrl(`/dgactualunit/info/${this.dataForm.unitCode}`),
              url: `/dg-standard-info/dgregisterunit/info/${this.dataForm.unitCode}`,
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.dgRegisterUnit.name
                this.dataForm.dwsxValue = data.dgRegisterUnit.institutions
                this.dataForm.qylbValue = data.dgRegisterUnit.businessCategory
                this.dataForm.qyczlxValue = data.dgRegisterUnit.typeBusiness
                this.dataForm.kgqkValue = data.dgRegisterUnit.holdings
                this.dataForm.jyfwValue = data.dgRegisterUnit.businessScope
                this.dataForm.monitoringValue = data.dgRegisterUnit.monitoring
                this.dataForm.businessLicense = data.dgRegisterUnit.businessLicense
                this.dataForm.addressId = data.dgRegisterUnit.addressId
                this.dataForm.addressText = data.dgRegisterUnit.addressName;
                // this.houseAndUnit.houseId = data.dgActualUnit.addressVo.houseId
                // this.houseAndUnit.houseName = data.dgActualUnit.addressVo.addressName
                // this.houseAndUnit.type = data.dgActualUnit.addressVo.dictValue
                // this.houseAndUnit.relationId = data.dgActualUnit.addressVo.relationId
              }
            })
          }
        })
      },
      addressDialogOpen () {
        this.addressVisible = true
        this.$nextTick(() => {
          this.$refs.addressDialog.init()
        })
      },
      validateBusiness (Code) {
        let patrn = /^[0-9A-Z]+$/;
        //18位校验及大写校验
        if ((Code.length != 18) || (patrn.test(Code) == false)) {
          console.info("不是有效的统一社会信用编码！");
          return false;
        }
        else {
          let Ancode;//统一社会信用代码的每一个值
          let Ancodevalue;//统一社会信用代码每一个值的权重
          let total = 0;
          let weightedfactors = [1, 3, 9, 27, 19, 26, 16, 17, 20, 29, 25, 13, 8, 24, 10, 30, 28];//加权因子
          let str = '0123456789ABCDEFGHJKLMNPQRTUWXY';
          //不用I、O、S、V、Z
          for (let i = 0; i < Code.length - 1; i++) {
            Ancode = Code.substring(i, i + 1);
            Ancodevalue = str.indexOf(Ancode);
            total = total + Ancodevalue * weightedfactors[i];
            //权重与加权因子相乘之和
          }
          let logiccheckcode = 31 - total % 31;
          if (logiccheckcode == 31) {
            logiccheckcode = 0;
          }
          let Str = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,T,U,W,X,Y";
          let Array_Str = Str.split(',');
          logiccheckcode = Array_Str[logiccheckcode];

          let checkcode = Code.substring(17, 18);
          if (logiccheckcode != checkcode) {
            console.info("不是有效的统一社会信用编码！");
            return false;
          }else{
            console.info("yes");
            return true;
          }
        }
      },
      getAddressParameters ( data ) {
        this.dataForm.addressId = data.selectId
        this.dataForm.addressText = data.selectText
      },
      // getAddressParameters ( data ) {
      //   this.dataForm.regionId = data.selectId
      //   this.dataForm.addressText = data.selectText
      // },
      // addressDialogOpen () {
      //   this.addressVisible = true
      //   this.$nextTick(() => {
      //     this.$refs.addressDialog.init()
      //   })
      // },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.houseAndUnit.type = this.dataForm.type
            this.loading = true
            this.$http({
              // url: this.$http.adornUrl(`/dgactualunit/${!this.dataForm.unitCode ? 'save' : 'update'}`),
              url: `/dg-standard-info/dgregisterunit/${!this.dataForm.unitCode ? 'save' : 'update'}`,
              method: 'post',
              data: this.$http.adornData({
                'unitCode': this.dataForm.unitCode || undefined,
                'name': this.dataForm.name,
                'institutions': this.dataForm.dwsxValue,
                'businessCategory': this.dataForm.qylbValue,
                'typeBusiness': this.dataForm.qyczlxValue,
                'holdings': this.dataForm.kgqkValue,
                'businessScope': this.dataForm.jyfwValue,
                'monitoring': this.dataForm.monitoringValue,
                'houseAndUnit': this.houseAndUnit,
                'businessLicense': this.dataForm.businessLicense,
                'addressId': this.dataForm.addressId,
                'sysCreateBy': this.$store.state.user.id,
                'sysUpdateBy': this.$store.state.user.id
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.loading = false
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                 this.loading = false
              }
            })
          }
        })
      },
      getSelectData () {
        getDictList('business_category').then(({data}) => {
          this.qylbOptions = data.dictList
        })
        getDictList('institutions').then(({data}) => {
          this.dwsxOptions = data.dictList
        })
        getDictList('type_business').then(({data}) => {
          this.qyczlxOptions = data.dictList
        })
        getDictList('holdings').then(({data}) => {
          this.kgqkOptions = data.dictList
        })
        getDictList('business_scope').then(({data}) => {
          this.jyfwOptions = data.dictList
        })
        getDictList('monitoring').then(({data}) => {
          this.monitoringOptions = data.dictList
        })
        getDictList('type').then(({data}) => {
          if (data && data.code === 0) {
            this.options = data.dictList
          }
        })
      }
    }
  }
</script>

<style>
/* .custom-3E8EF7 .el-form-item.is-required .el-form-item__label:before {
    font-size: 22px;
}
.custom-3E8EF7 .el-input.is-disabled .el-input__inner {
  color: #00000088;
} */
</style>

